export default {
    // 开启命名空间
    namespaced: true,
    // 用来存放数据的
    state: {
        text: "我是moduleReview",
        // 设置一个默认useId为0
        // hostname: "http://127.0.0.1:8080",
        // urlImg: "http://175.178.217.113:9000",
        hostname: "https://fine-food.xiamu.icu",
        urlImg: "https://fine-food.xiamu.icu",
        AuthorizationUrl: "http://175.178.217.113:63070",
        comment: {
            commentContent: '',
            commentStar: 0,
            foodId: 0,
            userId: 0,
            // userId: window.localStorage.getItem('userId'),
            userName: '肉豆蔻吖',
        }
    },
    actions: {
        updateUserId({ commit, rootState }) {
            const { hostname, urlImg } = rootState.moduleCommon
            return new Promise((resolve, reject) => {
                uni.request({
                    url: `${hostname}/api/users/getUsers`,
                    method: `post`,
                    header: {
                        'Authorization': 'Bearer ' + uni.getStorageSync("Authorization")
                    },
                    success: res => {
                        console.log('res', res);
                        if (res.statusCode === 401) { // token非法
                            uni.removeStorage({
                                key: 'Authorization'
                            })
                            resolve();
                            return;
                        }
                        commit("updateUserId", { "posts": res.data.data, urlImg });
                        resolve(); // 异步操作完成，使用 resolve 返回结果
                    },
                    fail: err => {
                        console.log('getUserInfo', err);
                        reject(err); // 异步操作失败，使用 reject 返回错误信息
                    }
                })
            })
        }
    },
    mutations: {
        updateUserId(state, { posts, urlImg }) {
            console.log('posts', posts);
            // console.log(state);
            state.comment.userId = posts.userId
            state.comment.userName = posts.userName
        }
    }
}